package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 所有的新增，修改，删除都用post
 * 查询用get
 */
@WebServlet("/back/user/findById")
public class UserFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.接收数据,json對象，里面的所有值应该全部转换为字符串
        String id = req.getParameter("id");
//        req.getParameterValues("");//这个是拿数组
//        3.封装对象（前端拿到的数据）
        UserModel userModel = new UserModel();
        userModel.setId(Integer.parseInt(id));
//        userModel.sete
//        4.调用service层
        UserService userService = new UserServiceImpl();
//        5.得到返回结果
        ResultModel resultModel = userService.findById(userModel);
//        6.将返回结果返回给前端
        resp.getWriter().println(JSONObject.toJSON(resultModel));
    }
}
